package com.dys.hejiayuncommunity.web.controller.common;

import cn.afterturn.easypoi.excel.entity.ExportParams;
import com.dys.hejiayuncommunity.common.core.controller.BaseController;
import com.dys.hejiayuncommunity.common.core.domain.BaseResponse;
import com.dys.hejiayuncommunity.common.utils.ExcelUtils;
import com.dys.hejiayuncommunity.community.domain.HjyCommunity;
import com.dys.hejiayuncommunity.community.domain.dto.HjyCommunityDTO;
import com.dys.hejiayuncommunity.community.domain.dto.HjyCommunityExcelDTO;
import com.dys.hejiayuncommunity.community.service.HjyCommunityService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpServletResponse;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @Author: 疾风
 * @ClassName: ExportExcelController
 * @Description: Excel导出控制器
 * @CreateDate: 2024/12/11
 * @Version: v1.0.0
 */
@Controller
@RequestMapping(value = {"/exportExcel"})
public class ExportExcelController extends BaseController {

    @Autowired
    private HjyCommunityService hjyCommunityService;

    // http://localhost:8888/exportExcel/exportCommunityExcel?pageNum=1&pageSize=10
    @GetMapping(value = {"/exportCommunityExcel"})
    public BaseResponse<String> exportCommunityExcel(HjyCommunity hjyCommunity, HttpServletResponse response) {
        // 分页
        startPage();

        List<HjyCommunityDTO> list = hjyCommunityService.findCommunityByCondition(hjyCommunity);
        // 进行数据转换
        List<HjyCommunityExcelDTO> hjyCommunityExcelDTOList = list.stream().map(hjyCommunityDTO -> {
            HjyCommunityExcelDTO excelDto = new HjyCommunityExcelDTO();
            excelDto.setCommunityId(hjyCommunityDTO.getCommunityId());
            excelDto.setCommunityCode(hjyCommunityDTO.getCommunityCode());
            excelDto.setCommunityName(hjyCommunityDTO.getCommunityName());
            excelDto.setCommunityProvinceName(hjyCommunityDTO.getCommunityProvinceName());
            excelDto.setCommunityTownName(hjyCommunityDTO.getCommunityTownName());
            excelDto.setCommunityCityName(hjyCommunityDTO.getCommunityCityName());
            excelDto.setCreateTime(hjyCommunityDTO.getCreateTime());
            excelDto.setRemark(hjyCommunityDTO.getRemark());
            return excelDto;
        }).collect(Collectors.toList());

        ExcelUtils.exportExcel(hjyCommunityExcelDTOList, HjyCommunityExcelDTO.class, "小区资料.xls", response, new ExportParams("小区信息列表", "小区信息"));

        return BaseResponse.success("导出小区信息成功");
    }
}
